//列表排序
var Sortcol = -1
var Sort_flg = [true, true, true, true]
//对id等于tableId的表按照第colIndex排列(colIndex从0开始)
function tablesort(tableId, colIndex) {
    var table = document.getElementById(tableId)
    var tableTbody = table.getElementsByTagName('tbody')[0]
    //取得表格的数据行数
    var table_tr = tableTbody.getElementsByTagName('tr')
    //把表格的各行数据存入临时数组，并进行排序
    Sortcol = colIndex
    var tempArray = new Array()
    for (var cnt = 0; cnt < table_tr.length; cnt++) {
        tempArray.push(table_tr[cnt])
    }
    tempArray.sort(sortfunc)
    //生成一个排好序的内容
    var fragment = document.createDocumentFragment()
    for (var cnt = 0; cnt < tempArray.length; cnt++) {
        fragment.appendChild(tempArray[cnt])
    }
    //把排好序的表格替代原表格
    tableTbody.appendChild(fragment)
    setCSSforTH(colIndex)
}
//改样式
function setCSSforTH(colIndex) {
    for (var cnt = 0; cnt < 4; cnt++) {
        document.getElementById('th' + cnt).style = '';
    }
    if (Sort_flg[colIndex]) {
        Sort_flg[colIndex] = false
        document.getElementById('th' + colIndex).style =
            'background-image:url(http://images.cnblogs.com/cnblogs_com/cloudgamer/169629/r_2.jpg);background-repeat:no-repeat;background-position:right center;';
    } else {
        Sort_flg[colIndex] = true
        document.getElementById('th' + colIndex).style =
            'background-image:url(http://images.cnblogs.com/cnblogs_com/cloudgamer/169629/r_1.jpg);background-repeat:no-repeat;background-position:right center;';
    }
}

function sortfunc(a, b) {
    var value1 = a.getElementsByTagName('td')[Sortcol].innerHTML;
    var value2 = b.getElementsByTagName('td')[Sortcol].innerHTML;

    if (Sort_flg[Sortcol]) {
        //升序排列
        if (isNaN(value1)) {
            return value1.localeCompare(value2) //字符串比较
        } else {
            return Number(value1) - Number(value2) //数值比较
        }
    } else {
        //降序排列
        if (isNaN(value1)) {
            return value2.localeCompare(value1)
        } else {
            return Number(value2) - Number(value1)
        }
    }
}
//使得选中一行的price和reserve可以编辑，并修改按钮样式
function editProduct(i,price,reserve) {
    var newId1="td1_"+i;
    var newId3="td3_"+i;
    var newBt1="commitBt_"+i;
    var newBt2="cancelBt_"+i;
    var newBt3="modifyBt_"+i;
    document.getElementById(newId1).innerHTML=
        '<td><div id='+newId1+'" contenteditable=\"True\">'+price+'</div></td>';
    document.getElementById(newId3).innerHTML=
        '<td><div id='+newId3+'" contenteditable=\"True\">'+reserve+'</div></td>';
    document.getElementById(newBt1).style.display="inline-block";
    document.getElementById(newBt2).style.display="inline-block";
    document.getElementById(newBt3).style.display="none";
}
//恢复按钮样式
function cancelTheChange(i,price,reserve) {
    var newId1="td1_"+i;
    var newId3="td3_"+i;
    var newBt1="commitBt_"+i;
    var newBt2="cancelBt_"+i;
    var newBt3="modifyBt_"+i;
    document.getElementById(newId1).innerHTML=
        '<td><div id='+newId1+'" contenteditable=\"False\">'+price+'</div></td>';
    document.getElementById(newId3).innerHTML=
        '<td><div id='+newId3+'" contenteditable=\"False\">'+reserve+'</div></td>';
    document.getElementById(newBt1).style.display="none";
    document.getElementById(newBt2).style.display="none";
    document.getElementById(newBt3).style.display="inline-block";
}
//保存修改内容，并恢复按钮样式
function saveTheChange(i,productId) {
    var newId1 = "td1_" + i;
    var newId3 = "td3_" + i;
    var newBt1 = "commitBt_" + i;
    var newBt2 = "cancelBt_" + i;
    var newBt3 = "modifyBt_" + i;
    var price = document.getElementById(newId1).innerText;
    var reserve = document.getElementById(newId3).innerText;
    document.getElementById(newId1).innerHTML =
        '<td><div id=' + newId1 + '" contenteditable=\"False\">' + price + '</div></td>';
    document.getElementById(newId3).innerHTML =
        '<td><div id=' + newId3 + '" contenteditable=\"False\">' + reserve + '</div></td>';
    document.getElementById(newBt1).style.display = "none";
    document.getElementById(newBt2).style.display = "none";
    document.getElementById(newBt3).style.display = "inline-block";
    //记录修改过的所有信息，是更新数据库的依据
    var newText=productId+","+price+","+reserve+"\n";
    document.getElementById('modifyProductInfo').value+=newText;
}
function addProductToDb(i) {
    var newId0 = "td0_" + i;
    var newId1 = "td1_" + i;
    var newId2 = "td2_" + i;
    var newId3 = "td3_" + i;
    var productName=document.getElementById(newId0).innerText;
    var price = document.getElementById(newId1).innerText;
    var sold=document.getElementById(newId2).innerText;
    var reserve = document.getElementById(newId3).innerText;
    location.href="/addProductToDb?productName="+productName+"&price="+price+"&sold="+sold+"&reserve="+reserve;
}
//新增一行
function addProduct(i){
    var newId0 = "td0_" + i;
    var newId1 = "td1_" + i;
    var newId2 = "td2_" + i;
    var newId3 = "td3_" + i;
    var  newtr =document.createElement("tr");
    newtr.id= new Date().getTime();
    newtr.innerHTML=
        ' <td id='+newId0+' " contenteditable=\"True\">空</td>\n' +
        ' <td id='+newId1+' " contenteditable=\"True\">0</td>\n' +
        ' <td id='+newId2+' " contenteditable=\"True\">0</td>\n' +
        ' <td id='+newId3+' " contenteditable=\"True\">0</td>\n' +
        ' <td></td>\n' +
        ' <td>' +
        '<button id="id1" onclick="addProductToDb('+i+')" style="width: auto" >保存</button>' +
        '<button id="id2" onclick="reShow()" style="width: auto" >取消</button>' +
        '</td>'
    //将新的一行加到列表中
    document.getElementById("tableSort").appendChild(newtr);
    //使得界面滑到底部
    location.href="#id2";
}
// 注销之前先保存修改
function logout() {
    var info=document.getElementById("modifyProductInfo").value;
    location.href="/saveProductToDb?type=0&info="+info;
}
//显示个人信息前先保存修改
function show1() {
    var info=document.getElementById("modifyProductInfo").value;
    location.href="/saveProductToDb?type=1&info="+info;
}
//重新展示商品界面前先保存修改
function reShow() {
    var info=document.getElementById('modifyProductInfo').value;
    location.href="/saveProductToDb?type=2&info="+info;
}
//显示用户购买日志之前先保存修改
function showUserHistory() {
    var info=document.getElementById("modifyProductInfo").value;
    location.href="/saveProductToDb?type=3&info="+info;
}


